package com.peking.donations.config;

import com.peking.donations.plugins.oauth2.client.EnableCustomerOAuth2;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

/**
 * Created by WYZ on 16/3/24.
 */
@Configuration
@EnableWebSecurity
@EnableCustomerOAuth2(config = "wechart")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(-101);
        return registration;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/third/wx/pay/**");
        web.ignoring().antMatchers("/static/**");
        web.ignoring().antMatchers("/error");
        web.ignoring().antMatchers("/resources");

        web.ignoring().antMatchers("/favicon.ico");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        RequestMatcher csrfRequestMatcher = new RequestMatcher() {
            private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");

            private AntPathRequestMatcher[] requestMatchers = { new AntPathRequestMatcher("/peking/**") };

            @Override
            public boolean matches(HttpServletRequest request) {
                if (allowedMethods.matcher(request.getMethod()).matches()) {
                    return false;
                }
                for (AntPathRequestMatcher rm : requestMatchers) {
                    if (rm.matches(request)) {
                        return false;
                    }
                }
                return true;
            }
        };
        http.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher);

        http.authorizeRequests().antMatchers("/peking/**").hasRole("USER");
        http.authorizeRequests().antMatchers("/info", "/env", "/metrics", "/trace", "/dump", "/mappings", "/autoconfig",
                "/beans", "/configprops").hasRole("ADMIN");
        // http.authorizeRequests().antMatchers("/**").hasRole("USER").and().csrf().ignoringAntMatchers("");
        http.csrf().disable();
        http.httpBasic().disable();
        // http.formLogin();
    }

    // @Bean(name = "wechartSuccessHandler")
    // AuthenticationSuccessHandler authenticationSuccessHandler(){
    // return new
    // }
}
